<?php
/**
 *author:韩帅
 *createtime:2017/6/21 16:35
 *description:
 */
namespace App\Http\Controllers\App;

use App\Models\User;
use Illuminate\Http\Request;
use App\Models\UserServerBalance;
use Illuminate\Support\Facades\DB;

class WidthdrawSystemController extends  BaseController
{
    public $status = [0 => '未审核',1 => '审核中',2 => '审核通过',3 => '已拒绝'];

    /**
     * 提现列表
     */
    public function widthdrawList(Request $request)
    {
        $return      = [];                       //存放查询出的数据
        $s_time      = $request->input('s_time');//开始时间
        $e_time      = $request->input('e_time');//结束时间
        $status      = $request->input('status',0);//状态,强制转换整形
        $user_mobile = $request->input('user_mobile');    //手机号
        $card_name   = $request->input('card_name');

        $obj       = new UserServerBalance();

        if($s_time > 0){

           $obj = $obj->where('created_at','>=',$s_time);
        }

        if($e_time > 0){

            $obj = $obj->where('created_at','<=',$e_time);
        }

        if (!empty($user_mobile)) {

            $obj = $obj->where('user_mobile',$user_mobile);
        }
        if (!empty($card_name)) {

            $obj = $obj->where('card_name',$card_name);
        }



        //10是全部
        if ($status == 10) {

        }else {
            $obj = $obj->where('status',$status);
        }

        $obj  = $obj->where('type',UserServerBalance::TYPE_WIDTHDRAW);//只看余额提现

        $data = $obj->orderBy('created_at','desc')->with('user')->paginate(10)->toArray();

        $return = $data;
        if (empty($return['data'])) {
            return self::json($return,'数据为空',200);
        }
        return self::json($return,'成功',200);
    }

    /**
     * 提现状态改为审核中
     */
    public function withdrawCheck(Request $request ) {

        $admin_id = $this->admin_id;
        $id       = intval($request->input('id'));

        if (empty($id)) {
            return self::error('订单ID不能为空');
        }

        //1.先查看提现订单的状态是否小于当前状态
        $obj = UserServerBalance::find($id);

        if (empty($obj)) {

            return self::error('记录不存在');
        }

        if ($obj->status !== UserServerBalance::STATUS_ONE) {

            return self::error('订单状态不是未审核');
        }

        //2.改变订单状态
        $obj->status   = UserServerBalance::STATUS_TWO;
        $obj->admin_id = $admin_id;

        $res = $obj->save();

       if ($res == true) {
           return self::success('成功');
       }
       return self::error('失败');
    }

    /**
     * 提现状态改为审核通过
     */
    public function withdrawOk(Request $request)
    {
        $admin_id = $this->admin_id;
        $id       = intval($request->input('id'));

        if (empty($id)) {
            return self::error('订单ID不能为空');
        }

        //1.先查看提现订单的状态是否小于当前状态
        $obj = UserServerBalance::find($id);

        if (empty($obj)) {

            return self::error('记录不存在');
        }

        if ($obj->status !== UserServerBalance::STATUS_TWO) {

            return self::error('订单状态不是审核中');
        }

        //2.改变订单状态
        $obj->status   = UserServerBalance::STATUS_THREE;
        $obj->admin_id = $admin_id;

        $res = $obj->save();

        if ($res == true) {
            return self::success('成功');
        }
        return self::error('失败');

    }
    /**
     * 提现状态改为审核不通过
     */
    public function withdrawFalse(Request $request)
    {

        $id   = intval($request->input('id'));
        $note = trim($request->input('note',''));

        if (empty($id)) {
            return self::error('订单ID不能为空');
        }

        if (empty($note)) {
            return self::error('备注不能为空');
        }

        //1.先查看提现订单的状态是否小于当前状态
        $obj  = UserServerBalance::find($id);

        if (empty($obj)) {
            return self::error('记录不存在');
        }

        //2.根据提现订单取出金额,给用户加余额
        $objB = User::find($obj->user_id);

        if (empty($objB)) {

            return self::error('用户记录不存在');
        }

        if ($obj->status !== UserServerBalance::STATUS_TWO) {

            return self::error('订单状态不是审核中');
        }

        //开事务
        $res = false;
        DB::beginTransaction();
        try{

            //3.改变订单状态
            $obj->status   = UserServerBalance::STATUS_FOUR;
            $obj->note     = $note;

            $obj->save();

            //4.给用户加余额
            $objB->balance = $objB->balance+$obj->money;
            $objB->save();

            // 提交事务
            DB::commit();
            $res = true;
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
        }

        if ($res == true) {
            return self::success('成功');
        }
        return self::json($e,'失败',400);
    }


    /**
     * excel导出列表
     */
    public function excel(){
        /* @实例化 */
        require app_path().'/Baqi/Excel/PHPExcel.php';
        require app_path().'/Baqi/Excel/PHPExcel/Writer/Excel5.php';
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->mergeCells('A1:I1');
        //设置font
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '郑州万店联盟电子商务有限公司');
        $objPHPExcel->getActiveSheet()->mergeCells('A2:F2');
        $objPHPExcel->getActiveSheet()->SetCellValue('A2', '日期:'.date('Y-m-d h:i:s',time()));
        $objPHPExcel->getActiveSheet()->mergeCells('G2:M2');
        $objPHPExcel->getActiveSheet()->SetCellValue('G2', '公司地址:河南省郑州市金水区凤凰台街道环球大厦B座升龙凤凰城A区');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(14);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->mergeCells('G1:I1');
        $objPHPExcel->getActiveSheet()->mergeCells('I3:J3');
        $objPHPExcel->getActiveSheet()->SetCellValue('A3', 'ID');
        $objPHPExcel->getActiveSheet()->SetCellValue('B3', '用户名');
        $objPHPExcel->getActiveSheet()->SetCellValue('C3', '模式');
        $objPHPExcel->getActiveSheet()->SetCellValue('D3', '手机号');
        $objPHPExcel->getActiveSheet()->SetCellValue('E3', '开户行');
        $objPHPExcel->getActiveSheet()->SetCellValue('F3', '银行卡号');
        $objPHPExcel->getActiveSheet()->SetCellValue('G3', '提现金额');
        $objPHPExcel->getActiveSheet()->SetCellValue('H3', '状态');
        $objPHPExcel->getActiveSheet()->SetCellValue('I3', '申请时间');

        $beginYesterday = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
        $endYesterday   = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y'))-1);

        //只取出类型为0的提现记录
        $obj  = UserServerBalance::where('type',0)->where('status',0);  //提现和未审核的
        $obj  = $obj->where('created_at','>',$beginYesterday)->where('created_at','<',$endYesterday);
        $info = $obj->orderBy('created_at','asc')->get()->toArray();

        foreach($info as $k => $v) {
            $k = $k+4;
            $objPHPExcel->getActiveSheet()->SetCellValue('A'.$k,$v['id']);
            $objPHPExcel->getActiveSheet()->SetCellValue('B'.$k,$v['card_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C'.$k,'银行卡提现');
            $objPHPExcel->getActiveSheet()->SetCellValue('D'.$k,' '.$v['user_mobile'].' ');
            $objPHPExcel->getActiveSheet()->SetCellValue('E'.$k,$v['card_tip']);
            $objPHPExcel->getActiveSheet()->SetCellValue('F'.$k,' '.$v['card_num']);
            $objPHPExcel->getActiveSheet()->SetCellValue('G'.$k,$v['money']);
            $objPHPExcel->getActiveSheet()->SetCellValue('H'.$k,$this->status[$v['status']]);
            $objPHPExcel->getActiveSheet()->SetCellValue('I'.$k,$v['created_at']);
        }

        //下面的合计
        $objPHPExcel->getActiveSheet()->setTitle('万店联盟债券金提现申请表');
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
//        $objWriter->save(str_replace('.php', '.xls', dirname(dirname(dirname(dirname(__FILE__)))).'/public/Userbalanceh.xls'));
        ob_end_clean();//清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=万店联盟债券金提现申请表.xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }

    /**
     * excel导出列表
     */
    public function excelMore(Request $request) {

        $num = intval($request->input('day'),0);

        if (empty($num)) {
            return self::error('请输入天数');
        }

        /* @实例化 */
        require app_path().'/Baqi/Excel/PHPExcel.php';
        require app_path().'/Baqi/Excel/PHPExcel/Writer/Excel5.php';
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->mergeCells('A1:I1');
        //设置font
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '郑州万店联盟电子商务有限公司');
        $objPHPExcel->getActiveSheet()->mergeCells('A2:F2');
        $objPHPExcel->getActiveSheet()->SetCellValue('A2', '日期:'.date('Y-m-d h:i:s',time()));
        $objPHPExcel->getActiveSheet()->mergeCells('G2:M2');
        $objPHPExcel->getActiveSheet()->SetCellValue('G2', '公司地址:河南省郑州市金水区凤凰台街道环球大厦B座升龙凤凰城A区');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(14);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->mergeCells('G1:I1');
        $objPHPExcel->getActiveSheet()->mergeCells('I3:J3');
        $objPHPExcel->getActiveSheet()->SetCellValue('A3', 'ID');
        $objPHPExcel->getActiveSheet()->SetCellValue('B3', '用户名');
        $objPHPExcel->getActiveSheet()->SetCellValue('C3', '模式');
        $objPHPExcel->getActiveSheet()->SetCellValue('D3', '手机号');
        $objPHPExcel->getActiveSheet()->SetCellValue('E3', '开户行');
        $objPHPExcel->getActiveSheet()->SetCellValue('F3', '银行卡号');
        $objPHPExcel->getActiveSheet()->SetCellValue('G3', '提现金额');
        $objPHPExcel->getActiveSheet()->SetCellValue('H3', '状态');
        $objPHPExcel->getActiveSheet()->SetCellValue('I3', '申请时间');

        $beginYesterday = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-$num,date('Y')));
        $endYesterday   = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y'))-$num);

        //只取出类型为0的提现记录
        $obj  = UserServerBalance::where('type',0)->where('status',0);  //提现和未审核的
        $obj  = $obj->where('created_at','>',$beginYesterday)->where('created_at','<',$endYesterday);
        $info = $obj->orderBy('created_at','asc')->get()->toArray();

        foreach($info as $k => $v) {
            $k = $k+4;
            $objPHPExcel->getActiveSheet()->SetCellValue('A'.$k,$v['id']);
            $objPHPExcel->getActiveSheet()->SetCellValue('B'.$k,$v['card_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C'.$k,'银行卡提现');
            $objPHPExcel->getActiveSheet()->SetCellValue('D'.$k,' '.$v['user_mobile'].' ');
            $objPHPExcel->getActiveSheet()->SetCellValue('E'.$k,$v['card_tip']);
            $objPHPExcel->getActiveSheet()->SetCellValue('F'.$k,' '.$v['card_num']);
            $objPHPExcel->getActiveSheet()->SetCellValue('G'.$k,$v['money']);
            $objPHPExcel->getActiveSheet()->SetCellValue('H'.$k,$this->status[$v['status']]);
            $objPHPExcel->getActiveSheet()->SetCellValue('I'.$k,$v['created_at']);
        }

        //下面的合计
        $objPHPExcel->getActiveSheet()->setTitle('万店联盟债券金提现申请表');
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
//        $objWriter->save(str_replace('.php', '.xls', dirname(dirname(dirname(dirname(__FILE__)))).'/public/Userbalanceh.xls'));
        ob_end_clean();//清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=万店联盟债券金提现申请表.xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }
}